package com.teadent.dao;

/*import com.teadent.bean.Person;
import com.teadent.bean.StudentTask;
import com.teadent.bean.Task;
import com.teadent.util.ConnectionFactory;*/

import com.teadent.bean.*;
import com.teadent.util.ConnectionFactory;
/*import org.apache.tomcat.dbcp.dbcp2.ConnectionFactory;*/

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao{
    @Override
    public List<Person> getAllStudent() {
        String sql="select * from person where userIdentify=0";
        return getAllStudent(sql);
    }

    @Override
    public List<Task> getAllTask() {
        String sql="select * from task";
        return getAllTask(sql);
    }

    @Override
    public List<StudentTask> getTaskDetail(String taskAccount) {
        return null;
    }

    @Override
    public void delThisStu(String userId) {
        String sql="delete from person where userId='"+userId+"'";
        Deal.deal(sql);
        sql="delete from studenttask where userId='"+userId+"'";
        Deal.deal(sql);
    }

    @Override
    public void addTask(Task task) {
        String sql="insert into task ( taskName, taskRequirement, taskDate) values ('"
                +task.getTaskName()+"','"+task.getTaskRequirement()+"','"+task.getTaskDate()+"')";
        Deal.deal(sql);

        sql="insert into studenttask(userId, taskAccount,taskName, isFinish, isScore)" +
                "select userId,taskAccount,'"+task.getTaskName()+
                "',0,0 from(select * from person,task\n" +
                "where userIdentify='0'&&taskName='"+task.getTaskName()+"')as B;";
        Deal.deal(sql);
    }

    @Override
    public void delThisTask(String taskAccount) {
        int a;
        a = Integer.valueOf(taskAccount);
        String sql="delete\n" +
                "from studenttask\n" +
                "where taskAccount="+a+";";
        Deal.deal(sql);
        sql="delete\n" +
                "from task\n" +
                "where taskAccount="+a+";";
        Deal.deal(sql);
    }

    @Override
    public Task getTask(String taskAccount) {
        String sql="select * from task where taskAccount='" +taskAccount +"'";
        return Deal.getTask(sql);
    }

    @Override
    public void fixTask(Task task,String taskAccount) {

        System.out.println("why?why?why?why?why?why?why?why?why?why?why?why?why?why?");
        System.out.println(task.getTaskName());
        System.out.println(task.getTaskAccount());
        System.out.println(task.getTaskName());
        String sql="update task set taskName='"+task.getTaskName()+"' ," +
                "taskRequirement='"+task.getTaskName()+"'," +
                "taskDate='"+task.getTaskName()+"' " +
                "where taskAccount="+taskAccount;
        Deal.deal(sql);
    }

    @Override
    public void addStudent(Person person0) {
        String sql="insert into person\n" +
                "(userName, userId, userAccount, userSex, userIdentify, userEmail, userPassword, userOthername)\n" +
                "values('"+person0.getUserName()+"','"+person0.getUserId()+
                "','"+person0.getUserAccount()+"','"+person0.getUserSex()+
                "','"+person0.getUserIdentify()+"','"+person0.getUserEmail()+
                "','"+person0.getUserPassword()+"','"+person0.getUserOthername()+"')";
        Deal.deal(sql);

    }

    private List<Task> getAllTask(String sql) {
        Connection connection= ConnectionFactory.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        List<Task> list=new ArrayList<>();
        try{
            pre=connection.prepareStatement(sql);
            res= pre.executeQuery();
            while (res.next()){
                Task newTask=new Task();
                newTask.setTaskAccount(res.getInt("taskAccount"));
                newTask.setTaskName(res.getString("taskName"));
                newTask.setTaskDate(res.getString("taskDate"));
                newTask.setTaskRequirement(res.getString("taskRequirement"));
                list.add(newTask);
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }finally {

        }
        return list;

    }

    private List<Person> getAllStudent(String sql) {
        Connection connection= ConnectionFactory.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        List<Person> list=new ArrayList<>();
        try{
            pre=connection.prepareStatement(sql);
            res= pre.executeQuery();
            while (res.next()){
                Person newPerson=new Person();
                newPerson.setUserName(res.getString("userName"));
                newPerson.setUserId(res.getString("userId"));
                newPerson.setUserSex(res.getString("userSex"));
                newPerson.setUserAccount(res.getString("userAccount"));
                newPerson.setUserPassword(res.getString("userPassword"));
                newPerson.setUserEmail(res.getString("userEmail"));
                newPerson.setUserIdentify(res.getInt("userIdentify"));
                newPerson.setUserOthername(res.getString("userOthername"));
                list.add(newPerson);
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }finally {

        }
        return list;
    }



}
